﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Office.Interop.Word;
using System.Windows.Input;
using System.Text.RegularExpressions;

namespace TestFYP
{
    class CountTemplate
    {
        protected Application app;
        protected Document doc;
        //protected int wordCount;
        //protected int symbolCount;
        //private object Filename;
        private Counters counter;
    
        public CountTemplate(Application app, Document doc)
        {
            this.app = app;
            this.doc = doc;
        }

        public CountTemplate(Application app, Document doc, Counters counter)
        {
            this.app = app;
            this.doc = doc;
            this.counter = counter;
        }


        public void doCount()
        {
            find(counter.range.Text);
            find(".,\\?;:\"'()\\!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        }

        private void find(String text)
        {
            try
            {                   
                MatchCollection Matches = Regex.Matches(text, "[.,\\?;:\"|'(|)|\\!|a-z|A-Z]");
                
                foreach(Match match in Matches)
                {
                    switch (match.Value)
                    {
                        case ",": counter.punc_comma++;
                                    break;
                        case ".": counter.punc_period++;
                                    break;
                        case "?": counter.punc_question++;
                                    break;
                        case ";": counter.punc_semicolon++;
                                    break;
                        case ":": counter.punc_colon++;
                                    break;
                        case "\"": counter.punc_invertcomma++;
                                    break;
                        case "(": counter.punc_OpenBracket++;
                                    break;
                        case ")": counter.punc_CloseBracket++;
                                    break;
                        case "!": counter.punc_exclamation++;
                                    break;
                        case "a": counter.graph_atoz[0]++;
                                    break;
                        case "b": counter.graph_atoz[1]++;
                                    break;
                        case "c": counter.graph_atoz[2]++;
                                    break;
                        case "d": counter.graph_atoz[3]++;
                                    break;
                        case "e": counter.graph_atoz[4]++;
                                    break;
                        case "f": counter.graph_atoz[5]++;
                                    break;
                        case "g": counter.graph_atoz[6]++;
                                    break;
                        case "h": counter.graph_atoz[7]++;
                                    break;
                        case "i": counter.graph_atoz[8]++;
                                    break;
                        case "j": counter.graph_atoz[9]++;
                                    break;
                        case "k": counter.graph_atoz[10]++;
                                    break;
                        case "l": counter.graph_atoz[11]++;
                                    break;
                        case "m": counter.graph_atoz[12]++;
                                    break;
                        case "n": counter.graph_atoz[13]++;
                                    break;
                        case "o": counter.graph_atoz[14]++;
                                    break;
                        case "p": counter.graph_atoz[15]++;
                                    break;
                        case "q": counter.graph_atoz[16]++;
                                    break;
                        case "r": counter.graph_atoz[17]++;
                                    break;
                        case "s": counter.graph_atoz[18]++;
                                    break;
                        case "t": counter.graph_atoz[19]++;
                                    break;
                        case "u": counter.graph_atoz[20]++;
                                    break;
                        case "v": counter.graph_atoz[21]++;
                                    break;
                        case "w": counter.graph_atoz[22]++;
                                    break;
                        case "x": counter.graph_atoz[23]++;
                                    break;
                        case "y": counter.graph_atoz[24]++;
                                    break;
                        case "z": counter.graph_atoz[25]++;
                                    break;


                        case "A": counter.graph_AtoZ[0]++;
                                    break;
                        case "B": counter.graph_AtoZ[1]++;
                                    break;
                        case "C": counter.graph_AtoZ[2]++;
                                    break;
                        case "D": counter.graph_AtoZ[3]++;
                                    break;
                        case "E": counter.graph_AtoZ[4]++;
                                    break;
                        case "F": counter.graph_AtoZ[5]++;
                                    break;
                        case "G": counter.graph_AtoZ[6]++;
                                    break;
                        case "H": counter.graph_AtoZ[7]++;
                                    break;
                        case "I": counter.graph_AtoZ[8]++;
                                    break;
                        case "J": counter.graph_AtoZ[9]++;
                                    break;
                        case "K": counter.graph_AtoZ[10]++;
                                    break;
                        case "L": counter.graph_AtoZ[11]++;
                                    break;
                        case "M": counter.graph_AtoZ[12]++;
                                    break;
                        case "N": counter.graph_AtoZ[13]++;
                                    break;
                        case "O": counter.graph_AtoZ[14]++;
                                    break;
                        case "P": counter.graph_AtoZ[15]++;
                                    break;
                        case "Q": counter.graph_AtoZ[16]++;
                                    break;
                        case "R": counter.graph_AtoZ[17]++;
                                    break;
                        case "S": counter.graph_AtoZ[18]++;
                                    break;
                        case "T": counter.graph_AtoZ[19]++;
                                    break;
                        case "U": counter.graph_AtoZ[20]++;
                                    break;
                        case "V": counter.graph_AtoZ[21]++;
                                    break;
                        case "W": counter.graph_AtoZ[22]++;
                                    break;
                        case "X": counter.graph_AtoZ[23]++;
                                    break;
                        case "Y": counter.graph_AtoZ[24]++;
                                    break;
                        case "Z": counter.graph_AtoZ[25]++;
                                    break;




                        default: break;
                    }    
                }




               
                 


            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                Console.WriteLine("end of doc reached" + e.ToString());
            }
            catch (Exception e) 
            {
                Console.WriteLine("unhandled "+ e.ToString());
            }
        }

        

        private void Wordtoword()
        {
            object oEndOfDoc = "\\endofdoc";
            object oRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            object ignore = false;
            int i = 1, y = doc.ComputeStatistics(WdStatistic.wdStatisticWords, ref ignore), z = app.Selection.Words.Count;
            while (i != z)
            {
                doc.Words[i++].Select();
                Console.WriteLine("i found " + app.Selection.Text.ToString());
            }
        }


    }
}
